<script>
import UniPopup from "@/components/no-mask-popup/uni-popup.vue";
import analysisVideo from "../../analysisVideo.vue";
import closeIcon from "../../../assets/close.png";
import common from "../../common";
export default {
  name: "answerSheet",
  components: { analysisVideo, UniPopup },
  watch: {
    viewAnalysis: {
      handler(val) {
        if (val) {
          this.viewAnalysisToggle = "题目选项";
        } else {
          this.viewAnalysisToggle = "做题模式";
        }
      },
      immediate: true,
    },
    currentQuestionIndex: {
      handler(val) {
        this.currentQuestion = this.parent.questionList[val];
      },
      immediate: true,
    },
  },
  data() {
    return {
      closeIcon,
			common,
      lastBtn: "", //用于标识用户点击的上一个按钮，以提供相应的样式效果进行标识，增强用户体验
      viewAnalysisToggle: "题目选项",
      currentQuestion: null,
    };
  },
  props: {
    viewAnalysis: Boolean,
    currentQuestionIndex: Number,
    questionList: Array,
  },
  methods: {
    open() {
      this.$refs["answer-sheet"].open();
    },
    // 切换到上一题
    prevQuestion() {
      this.$emit("update:currentQuestionIndex", this.currentQuestionIndex - 1);
    },
    //切换到下一题
    nextQuestion() {
      if (this.currentQuestionIndex < this.questionList.length - 1) {
        this.$emit(
          "update:currentQuestionIndex",
          this.currentQuestionIndex + 1,
        );
      }
    },
    //选择了题目选项
    questionOptionSelect(item) {
      this.$set(this.currentQuestion, "userAnswer", item.tag);
      this.currentQuestion.doRight = item.answer;
      //自动跳到下一题
      setTimeout(() => {
        this.nextQuestion();
      }, 500);
    },
    getOptionClass(answer, userAnswer) {
      if (answer) return "success";
      if (userAnswer) return "error";
    },
  },
  inject: ["root", "parent"],
  emits: ["submit", "update:currentQuestionIndex"],
};
</script>

<template>
  <uni-popup
    ref="answer-sheet"
    type="bottom"
    :is-mask-click="false"
    mask-background-color="transparent"
  >
    <view class="answer-sheet-content">
      <view class="header">
        <view>
          <!--						查看解析模式：切换题目选项和解析-->
          <view class="tagList" v-if="viewAnalysisToggle !== '做题模式'">
            <view
              class="tag-item"
              :class="{ active: viewAnalysisToggle === '题目选项' }"
              @click="viewAnalysisToggle = '题目选项'"
              >题目选项
            </view>
            <view
              class="tag-item"
              :class="{ active: viewAnalysisToggle === '解析' }"
              @click="root.checkIsVip() && (viewAnalysisToggle = '解析')"
              >解析
            </view>
          </view>
        </view>
        <image
          class="icon"
          :src="closeIcon"
          @click="$refs['answer-sheet'].close()"
        ></image>
      </view>
      <!--				题目名称-->
      <view class="question-name">
        <text>{{ currentQuestion.serialNum }}</text>
        <text>.\t</text>
        <text
          style="color: darkred"
          v-if="
            viewAnalysisToggle !== '做题模式' &&
            common.handleOptionList(currentQuestion.childQuestions) === 'noDo'
          "
          >\t\t【未作答】</text
        >
      </view>
      <view class="content">
        <!--				选项列表-->
        <block v-if="['做题模式', '题目选项'].includes(viewAnalysisToggle)">
          <view class="option-list">
            <block
              v-for="item in currentQuestion.childQuestions"
              :key="item.tag"
            >
              <!--						做题模式-->
              <view
                class="option-item"
                v-if="viewAnalysisToggle === '做题模式'"
                @click="
                  () => {
                    questionOptionSelect(item);
                  }
                "
              >
                <view
                  class="mark"
                  :class="{ active: currentQuestion.userAnswer === item.tag }"
                  >{{ item.tag }}
                </view>
                <view class="content">{{ item.content }}</view>
              </view>
              <!--						查看解析模式-->
              <view v-else class="option-item">
                <view
                  :class="
                    'mark ' + getOptionClass(item.answer, item.userAnswer)
                  "
                  >{{ item.tag }}
                </view>
                <view class="content">
                  <view>{{ item.content }}</view>
                  <!--							选项翻译-->
                  <view
                    class="option-trans"
                    v-if="viewAnalysisToggle === '题目选项' && item.trans"
                  >
                    {{ item.trans }}
                  </view>
                </view>
              </view>
            </block>
          </view>
        </block>
        <!--				题目解析-->
        <view class="analysis" v-else-if="viewAnalysisToggle === '解析'">
          <view
            v-html="currentQuestion.analysis"
            v-if="currentQuestion.analysis"
          ></view>
          <view style="margin-top: 20rpx" v-if="currentQuestion.videoUrl">
            <analysisVideo
              :video-file-url="currentQuestion.videoUrl"
            ></analysisVideo>
          </view>
        </view>
      </view>
      <view class="footer">
        <view class="btn" @click="parent.$refs.answerRecord.open(questionList)"
          >题目一览
        </view>
        <view
          class="btn"
          @click="
            lastBtn = 'prevQuestion';
            prevQuestion();
          "
          :class="[lastBtn === 'prevQuestion' ? 'active' : '']"
          v-if="currentQuestionIndex > 0"
          >上一题
        </view>
        <!--					做第一题时把上一题按钮禁用掉-->
        <view class="btn disable" v-else>上一题</view>
        <view
          class="btn"
          @click="
            lastBtn = 'nextQuestion';
            nextQuestion();
          "
          :class="[lastBtn === 'nextQuestion' ? 'active' : '']"
          v-if="currentQuestionIndex < questionList.length - 1"
          >下一题
        </view>
        <!--					最后一道题显示提交按钮-->
        <view
          class="btn"
          v-else-if="!viewAnalysis"
          @click="
            () => {
              $emit('submit');
            }
          "
          >完成作答
        </view>
        <view class="btn disable" v-else>下一题</view>
      </view>
    </view>
  </uni-popup>
</template>

<style scoped lang="scss">
.answer-sheet-content {
  background: white;
  border-radius: 40rpx 40rpx 0 0;
  padding: {
    top: 30rpx;
    left: 30rpx;
    right: 30rpx;
  }
  border: 4rpx inset #999999;
  height: 726rpx;
  display: flex;
  flex-direction: column;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .tagList {
      display: flex;
      .tag-item {
        font-size: calc(36rpx * var(--font-size-rate));
        color: var(--text-color-light);
        &:not(:first-child) {
          margin-left: 70rpx;
        }
        &.active {
          color: var(--text-color);
          font-weight: bold;
        }
      }
    }
    > .icon {
      width: 60rpx;
      height: 60rpx;
    }
  }
  .question-name {
    margin-top: 20rpx;
    font-size: calc(34rpx * var(--font-size-rate));
    font-weight: bold;
  }
  > .content {
    height: 0;
    flex: 1;
    overflow-y: scroll;
    .option-list {
      .option-item {
        display: flex;
        align-items: center;
        margin-top: 30rpx;
        .mark {
          height: 80rpx;
          display: flex;
          align-items: center;
          padding: 0 40rpx;
          margin-right: 20rpx;
          background: var(--gray-background-color);
          border-radius: 40rpx;
          font-size: calc(34rpx * var(--font-size-rate));
          &.active {
            background: var(--subtheme-color);
          }
          &.success {
            background: var(--success-color);
          }
          &.error {
            background: var(--error-color);
          }
        }
        .content {
          font-size: calc(30rpx * var(--font-size-rate));
          .option-trans {
            color: var(--text-color-light);
            font-size: calc(30rpx * var(--font-size-rate));
          }
        }
      }
    }
    > .analysis {
      font-size: calc(32rpx * var(--font-size-rate));
      line-height: 2;
      margin-top: 20rpx;
    }
  }
  > .footer {
    display: flex;
    justify-content: space-around;
    padding-bottom: 20rpx;
    .btn {
      height: 60rpx;
      display: flex;
      align-items: center;
      border-radius: 40rpx;
      padding: 0 40rpx;
      border: 4rpx solid #999999;
      font-size: calc(30rpx * var(--font-size-rate));
      background: var(--theme-color-light);
      &.disable {
        opacity: 0.5;
      }
      &.active {
        border: 4rpx solid var(--text-color-theme);
      }
    }
  }
}
</style>
